// pages/zhongcao_detail/zhongcao_detail.js
const $api = require('../utils/api').API
// 获取应用实例
const app = getApp()
const {
    baseURL
} = require('../utils/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swipeList: ['../../img/zhongcao_pic.jpg','../../img/huazhong.png','../../img/zhongcao_pic.jpg'],
    contentItem: null,
    collectItem: null,
    loveItem: null,
    loveStatus: false,
    collectStatus: false,
    throttleCollect: null,
    throttleLove: null,
    contentId: null,
    setData: null,
  },

  throttle(fn, delay = 1000) {
    let timer = null
    return function () {
        if(timer) return
        fn.apply(this,arguments)
        timer = setTimeout(() => {
          timer = null
        },delay)
    }
 },

  clickLove(){
    let data = { 
        "type": 1, //1喜欢、2收藏
        "userId": wx.getStorageSync('user').userId,
        "moduleId": 1, //1、内容，2、民宿
        "outerId": this.data.contentId, 
    } 
    if(null != this.data.loveItem){
        data.id = this.data.loveItem.id
    }
    // console.log('this.data.loveItem.id:',this.data.loveItem.id);
    this.data.throttleLove(data);
  },
  clickCollect(){
    let data = { 
        "type": 2, //1喜欢、2收藏
        "userId": wx.getStorageSync('user').userId,
        "moduleId": 1, //1、内容，2、民宿
        "outerId": this.data.contentId, 
    }
    if(null != this.data.collectItem){
        data.id = this.data.collectItem.id
    }    
    // console.log('this.data.collectItem.id:',this.data.collectItem.id);
    this.data.throttleCollect(data);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    console.log(decodeURIComponent(options.id)) //'1' ,无论传的变量是数字、布尔还是对象接收的时候都是字符串
    let id = decodeURIComponent(options.id);
    this.setData({
        contentId : id
    })
    let moduleId = decodeURIComponent(options.moduleId);
    $api.selectContentById(id).then(res =>{
        console.log(res.data.data);
        console.log(baseURL);
        let contentItem = res.data.data
        contentItem.peopleSrc = baseURL + contentItem.peopleSrc
        console.log(contentItem.src);
        this.setData({
            contentItem: res.data.data
        })
    })
    console.log("app.globalData",app.globalData);
    let data = {
        "moduleId": 1, 
        "userId": wx.getStorageSync('user').userId,
        "deleteFlag": 0,
        "outerId": id
    }
    //获取用户关注收藏信息
    $api.getSelfInterestList(data).then(res=>{
        console.log(res.data.rows);
        res.data.rows.forEach(item=>{
            if(item.type == 1) {
                this.setData({loveStatus: true})
                this.data.loveItem = item
            }
            if(item.type == 2) {
                this.setData({collectStatus: true})
                this.data.collectItem = item
            }

        })
        console.log(this.data.loveStatus);
        console.log(this.data.collectStatus);
    })

    //节流函数使用
    this.data.throttleCollect = this.throttle(function fn(data){
        if(this.collectStatus == false){
            Reflect.deleteProperty(data, 'id')
            this.setData({collectStatus: true}) 
            $api.addSelfInterestList(data).then(res=>{
                if(res.data.code==200){             
                    wx.showToast({
                        title: '添加到收藏',
                        icon: 'success'
                    })
                    this.setData({collectItem: res.data.data})
                    this.contentItem.searchCollect +=1;
                    this.setData({contentItem: this.contentItem})
                }else{
                    wx.showToast({
                        title: `${res.data.msg}`,
                        })
                    this.setData({collectStatus: false})
                }
            })
        }else{
            this.setData({collectStatus: false})
            $api.deleteSelfInterestList(data.id).then(res=>{
                if(res.data.code==200){ 
                    this.contentItem.searchCollect -=1;
                    this.setData({contentItem: this.contentItem})
                }else{
                    wx.showToast({
                        title: `${res.data.msg}`,
                        })
                    this.setData({collectStatus: true})
                }  
            })
        }
    })
    //喜欢按钮 节流
    this.data.throttleLove = this.throttle(function fn(data){
        console.log(this);
        if(this.loveStatus == false){
            this.setData({loveStatus: true})
            Reflect.deleteProperty(data, 'id')
            $api.addSelfInterestList(data).then(res=>{
                if(res.data.code==200){                    
                    wx.showToast({
                        title: '已添加喜欢',
                        icon: 'success'
                    })
                    this.setData({loveItem: res.data.data})
                    this.contentItem.searchLove +=1;
                    this.setData({contentItem: this.contentItem})
                }else{
                    wx.showToast({
                        title: `${res.data.msg}`,
                        })
                    this.setData({loveStatus: false})
                }
            })
        }else{
            this.setData({loveStatus: false})
            $api.deleteSelfInterestList(data.id).then(res=>{
                if(res.data.code==200){ 
                    this.contentItem.searchLove -=1;
                    this.setData({contentItem: this.contentItem})
                }else{
                    wx.showToast({
                        title: `${res.data.msg}`,
                        })
                    this.setData({loveStatus: true})
                        
                }  
            })
        }
    })

    //
    let that = this
    this.data.setData = function (obj){
        for (let objKey in obj) {
            console.log(objKey)
            let objK = `${objKey}`
                that.setData({[objK]: obj[objKey]})
        console.log('that:',that.data[objKey]);
        }
        console.log('obj:',obj);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})